<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Customer_model extends MY_Model{
	public function __construct()
    {
        parent::__construct();
        $this->table='pm_custmoer';
    }
    public function ajax_lists($where=array(),$field='A.*,B.displayname as displayname,B.name as saleuname',$order='A.id desc'){//G.id as referid,H.*
        $this->com_where($where);
        $this->db->select('count(id) as counts');
        $query=$this->db->get($this->table." as A");
        $counts=$query->row_array();
        // $pagesize=$this->pagesize;
        $pagesize=10;
        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$pagesize * ($page - 1);
        $this->com_where($where);
        $this->db->from($this->table." as A");
        $this->db->join('pm_user as B',"A.saleuid=B.id",'left');
        $this->db->order_by($order);
        $this->db->limit($pagesize,$startRow);
        $this->db->select($field);
        $query=$this->db->get();
        $list=$query->result_array();
        $data['list']=$list; 
        $data['pageshow']=$this->pageajax_show($pagesize,$counts['counts']);
        return $data;
    }
	public function all($where=array(),$field='*',$order='id desc'){
        $this->com_where($where);
        $this->db->from($this->table);
        $this->db->order_by($order);
        $this->db->select($field);
        $query=$this->db->get();
        $list=$query->result_array();
        return $list;
    }
	public function one($where=array(),$field='A.*,B.displayname as displayname,B.name as saleuname'){
		$this->com_where($where);
		$this->db->select($field);
		$this->db->from($this->table." as A");
        $this->db->join('pm_user as B',"A.saleuid=B.id",'left');
		$query=$this->db->get();
		return $query->row_array();
	}
	public function add($data){
		if($this->db->insert($this->table,$data)){
			$data['id']=$this->db->insert_id();
			return $data;
		}else{
			$this->db->trans_commit();
			return false;
		}
	}
	public function edit($where=array(),$data){
		$this->com_where($where);
        $this->db->update($this->table,$data);
        if($this->db->affected_rows()>0){
            return true;
        }else{
            return false;
        }
	}
	public function del($where=array()){
		$this->com_where($where);
        if($this->db->delete($this->table)!==false){
            return true;
        }else{
            return false;
        }
	}
	public function user_all($where=array(),$field='*',$order='id desc'){
        $this->com_where($where);
        $this->db->from("pm_user");
        $this->db->order_by($order);
        $this->db->select($field);
        $query=$this->db->get();
        $list=$query->result_array();
        return $list;
    }
	public function user_one($where=array(),$field='*'){
		$this->com_where($where);
		$this->db->select($field);
        $this->db->from("pm_user");
		$query=$this->db->get();
		return $query->row_array();
	}
	//查询客服项目目录是否使用
	public function getIsProject($custmoerid,$field='*'){
		$this->db->where('custmoerid',$custmoerid);
		$this->db->select($field);
		$query=$this->db->get('pm_project');
		return $query->row_array();
	}
}
?>